package com.goudan.practice.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * 多线程交替打印 1~100
 * LockSupport 实现
 */
public class ThreadPrint2 {

    private static int number = 1;
    private static Thread thread1, thread2,thread3;

    public static void main(String[] args) {
        thread1 = new Thread(ThreadPrint2::thread1, "thread1");
        thread2 = new Thread(ThreadPrint2::thread2, "thread2");
        thread3 = new Thread(ThreadPrint2::thread3, "thread3");
        thread1.start();
        thread2.start();
        thread3.start();
    }

    public static void thread1() {
        while (ThreadPrint2.number <= 100) {
            System.out.println("thread1:" + ThreadPrint2.number);
            ThreadPrint2.number++;
            LockSupport.unpark(thread2);
            LockSupport.park();
        }
    }

    public static void thread2() {
        while (ThreadPrint2.number <= 100) {
            LockSupport.park();
            System.out.println("thread2:" + ThreadPrint2.number);
            ThreadPrint2.number++;
            LockSupport.unpark(thread3);
        }
    }

    public static void thread3() {
        while (ThreadPrint2.number <= 100) {
            LockSupport.park();
            System.out.println("thread3:" + ThreadPrint2.number);
            ThreadPrint2.number++;
            LockSupport.unpark(thread1);
        }
    }

}





